/**
 * FileName: AirRideQRCodeServiceImpl
 * Author:   Leopold
 * Date:     2018/9/14 14:57
 * Description:
 * History:
 */
package cn.mcsys.myadmin.modules.viewer.service.impl;

import cn.mcsys.myadmin.common.utils.QrCodeUtils;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.viewer.dao.AirRideQrCodeDao;
import cn.mcsys.myadmin.modules.viewer.entity.AirRideQrCode;
import cn.mcsys.myadmin.modules.viewer.entity.AirViewerUser;
import cn.mcsys.myadmin.modules.viewer.service.AirRideQRCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AirRideQRCodeServiceImpl implements AirRideQRCodeService {

    public static final String FILEDIR = "QRCodePicture";

    private static Logger logger = LoggerFactory.getLogger(AirRideQRCodeServiceImpl.class);

    @Autowired
    private AirRideQrCodeDao airRideQrCodeDao;

    @Autowired
    private ServletContext servletContext;

    @Override
    public AirRideQrCode save(Long ridePoint, Long[] eTicketsId, String bookingDate, String phone, AirViewerUser user) {
        AirRideQrCode airRideQrCode = new AirRideQrCode();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date bDate = null;
        try {
            bDate = simpleDateFormat.parse(bookingDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        AirRidePoint airRidePoint = new AirRidePoint();
        airRidePoint.setId(ridePoint.intValue());
        airRideQrCode.setAirRidePoint(airRidePoint);
        airRideQrCode.setCreateDate(new Date());
        airRideQrCode.setPhone(phone);
        airRideQrCode.setUser(user);
        airRideQrCode.setRideDate(bDate);
        airRideQrCode.setRideNo(eTicketsId.length);
        airRideQrCodeDao.insert(airRideQrCode);
        //生成二维码,日期需要修改
        String content = "2018airshow_zhits/date=" + bookingDate + "/no=" + eTicketsId.length + "/id=" + airRideQrCode.getId();
        //当前项目路径
        String path = servletContext.getRealPath("/");
        path += FILEDIR;
        File qrDir = new File(path);
        if (qrDir.exists()) {
            qrDir.mkdir();
        }
        System.out.println(path);
        String filename = null;
        try {
            filename = QrCodeUtils.createQRCode(content, path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        airRideQrCode.setPictureUrl("/" + FILEDIR + "/" + filename);
        airRideQrCodeDao.update(airRideQrCode);
        return airRideQrCode;
    }

    @Override
    public AirRideQrCode get(AirRideQrCode airRideQrCode) {
        return airRideQrCodeDao.get(airRideQrCode.getId());
    }

    @Override
    public List<AirRideQrCode> selectByUserId(Long id) {
        return airRideQrCodeDao.selectByUserId(id);
    }

    public Integer selectCountByPointId(Integer id, String bookDate) {
        Integer count = 0;
        if ("all".equals(bookDate)) {
            Integer pointCount = airRideQrCodeDao.selectCountByPointId(id, "2018-11-09");
            if (pointCount == null) {
                pointCount = 0;
            }
            count += pointCount;
            pointCount = airRideQrCodeDao.selectCountByPointId(id, "2018-11-10");
            if (pointCount == null) {
                pointCount = 0;
            }
            count += pointCount;
            pointCount = airRideQrCodeDao.selectCountByPointId(id, "2018-11-11");
            if (pointCount == null) {
                pointCount = 0;
            }
            count += pointCount;
        } else {
            count = airRideQrCodeDao.selectCountByPointId(id, bookDate);
            if (count == null) {
                count = 0;
            }
        }
        return count;
    }

    @Override
    public List<String> selectByPage(int pageNo) {
        return airRideQrCodeDao.selectByPage(pageNo);
    }
}
